<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>箭头函数</title>
</head>

<body>
    <script>
        // 参数只有一个时小括号可以省略，代码块只有一行时大括号和return可以省略
        const getSum = (x, y) => x + y
        getSum(1, 2)

        // 返回一个对象
        const fn = username => ({username: username})
        console.log(fn('张三'));

        // 箭头函数没有自己的this 箭头函数的this指向是上一层作用域的this指向
        const fn1 = () => console.log(this);
        fn1() // window

        const obj = {
            username: '张三',
            sayHi: () => console.log(this)
        }

        obj.sayHi() // window

        // 箭头函数没有自己的this 箭头函数的this指向是上一层作用域的this指向
        const studen = {
            studenName: '李四',
            sayHi: function(){
                console.log(this) // studen
                const count = () => {
                    console.log(this) // studen
                }
                count()
            }
        }

        studen.sayHi() // studen
    </script>
</body>

</html>